﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace seiacpscs.sortalgorithms
{
    class BubbleSort : SortAlgorithm
    {

        public BubbleSort()
        {
            this.algorithmName = "Bubble Sort";
        }


        public override void Sort(int[] items)
        {

            this.numberOfInnerLoops = 0; // Statistics Code
            this.numberOfOuterLoops = 0; // Statistics Code


            int temp;
            bool swapped;

            Stopwatch stopWatch = new Stopwatch(); // Statistics Code
            stopWatch.Start(); // Statistics Code

            do
            {
                ++(this.numberOfOuterLoops); // Statistics Code
                swapped = false;
                for (int i = 1; i < items.Length; i++)
                {
                    ++(this.numberOfInnerLoops); // Statistics Code
                    if (items[i - 1] > items[i])
                    {
                        ++(this.numberOfSwaps);
                        // Swapping Values
                        temp = items[i - 1];
                        items[i - 1] = items[i];
                        items[i] = temp;
                        swapped = true;
                    }
                }

            } while (swapped == true);

            stopWatch.Stop(); // Statistics Code
            this.executionTime = (stopWatch.ElapsedTicks * 1000L * 1000L * 1000L) / Stopwatch.Frequency; // Statistics Code

        }
    }
}
